package ecom.beans;

import ecom.beans.dao.AccountDAO;
import ecom.beans.dao.ProductDAO;
import ecom.beans.dao.ProductcategoryDAO;
import ecom.beans.dao.ProductstoreDAO;
import ecom.beans.dao.StockDAO;
import ecom.beans.dao.TransactionDAO;
import ecom.beans.dao.UserDAO;
import ecom.beans.exception.AccountBeanException;
import ecom.beans.exception.DAONotFoundException;
import ecom.beans.exception.DAONotUniqueException;
import ecom.beans.exception.ProductBeanException;
import ecom.beans.exception.ProductcategoryException;
import ecom.beans.exception.ProductstoreBeanException;
import ecom.beans.exception.StockBeanException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.ejb.Stateful;

@Stateful
public class EcomAdminBean extends EcomAdminUserBean implements EcomAdminRemote, Serializable {

    @Override
    public Set<AccountBean> getAllAccounts() {
        return AccountDAO.getAllAccounts(null);
    }

    @Override
    public Set<UserBean> getAllUsers() {
        return UserDAO.getAllUsers(null);
    }

    @Override
    public Long createProductStore(String name, String adress, String postalCode, String city, String tel, String iban, Double balance) throws DAONotUniqueException, AccountBeanException, ProductstoreBeanException {
        AccountBean account = new AccountBean(iban, balance);
        ProductstoreBean productstore = new ProductstoreBean(name, adress, postalCode, city, tel, account);

        String exception = "";
        boolean isException = false;
        if (AccountDAO.getByIBAN(iban) == null) {
            if (iban.length() > 34 || iban.length() < 34) {
                throw new AccountBeanException("IBAN must contain 34 characters");
            }
            if (name.length() > 50) {
                isException = true;
                exception += exception.equals("") ? "The name of the productstore should not exceed 50 characters"
                        : " ,the name of the productstore should not exceed 50 characters";
            }
            if (adress.length() > 100) {
                isException = true;
                exception += exception.equals("") ? "The adress of the productstore should not exceed 100 characters"
                        : " ,the adress of the productstore should not exceed 100 characters";
            }
            if (postalCode.length() > 10) {
                isException = true;
                exception += exception.equals("") ? "The postal code of the productstore should not exceed 10 characters"
                        : " ,the postal code of the productstore should not exceed 10 characters";
            }
            if (city.length() > 50) {
                isException = true;
                exception += exception.equals("") ? "The city of the productstore should not exceed 50 characters"
                        : " ,the city of the productstore should not exceed 50 characters";
            }
            if (tel.length() > 20) {
                isException = true;
                exception += exception.equals("") ? "The phone number of the productstore should not exceed 20 characters"
                        : " ,the phone number of the productstore should not exceed 20 characters";
            }
            if (isException) {
                throw new ProductstoreBeanException(exception);
            }

            List objectList = new ArrayList();
            objectList.add(account);
            objectList.add(productstore);
            TransactionDAO.saveOrUpdateAllObjects(objectList);
        } else {
            throw new DAONotUniqueException("This account (iban : " + iban + " already exist");
        }
        return productstore.getIdProductStore();
    }

    @Override
    public Long createProductcategory(String name) throws DAONotUniqueException, ProductcategoryException {
        ProductcategoryBean productcategory = new ProductcategoryBean(name);
        return ProductcategoryDAO.saveProductcategory(productcategory);
    }

    @Override
    public Long createProduct(String name, Long idProductcategory) throws ProductBeanException {
        ProductcategoryBean productcategory = ProductcategoryDAO.getByIdProductcategory(idProductcategory);
        ProductBean product = new ProductBean(name, productcategory);
        return ProductDAO.saveProduct(product);
    }

    @Override
    public Long createEntireProduct(String name, String description, String urlPhoto, Long idProductcategory) throws ProductBeanException {
        ProductcategoryBean productcategory = ProductcategoryDAO.getByIdProductcategory(idProductcategory);
        ProductBean product = new ProductBean(name, description, urlPhoto, productcategory);
        return ProductDAO.saveProduct(product);
    }

    @Override
    public void createStock(Long idProductstore, Long idProduct, Integer quantity, Double price) throws DAONotUniqueException {
        ProductstoreBean productstore = ProductstoreDAO.getByIdProductstore(idProductstore);
        ProductBean product = ProductDAO.getByIdProduct(idProduct);
        StockBean stock = new StockBean(productstore, product, quantity, price);
        StockDAO.saveStock(stock);
    }

    @Override
    public void removeStock(Long idProductstore, Long idProduct) throws StockBeanException, DAONotFoundException {
        try {
            StockBean stock = StockDAO.getByIdProductstoreAndIdProduct(idProduct, idProductstore);
            StockDAO.deleteStock(stock);
        } catch (NullPointerException ex) {
            throw new DAONotFoundException("The stock has not been found");
        }
    }

    @Override
    public void updateProductstore(Long idProductstore, String name, String adress, String postalCode, String city, String tel) throws DAONotFoundException {
        ProductstoreBean productStore = ProductstoreDAO.getByIdProductstore(idProductstore);
        if (productStore != null) {
            productStore.setAdress(adress);
            productStore.setCity(city);
            productStore.setName(name);
            productStore.setTel(tel);
            productStore.setPostalCode(postalCode);
        } else {
            throw new DAONotFoundException("The productstore has not been found");
        }
    }

    @Override
    public void updateProduct(Long idProduct, String name, String description, String urlPhoto, Long idProductcategory) throws DAONotFoundException {
        ProductcategoryBean category = ProductcategoryDAO.getByIdProductcategory(idProductcategory);
        if (category != null) {
            ProductBean product = ProductDAO.getByIdProduct(idProduct);
            if (product != null) {
                product.setIdProductCategory(category);
                product.setDescription(description);
                product.setName(name);
                product.setUrlPhoto(urlPhoto);
            } else {
                throw new DAONotFoundException("The product has not been found");
            }
        } else {
            throw new DAONotFoundException("The productcategory has not been found");
        }
    }

    @Override
    public void updateProductcategory(Long idProductcategory, String name) throws DAONotFoundException {
        ProductcategoryBean productcategory = ProductcategoryDAO.getByIdProductcategory(idProductcategory);
        if (productcategory != null) {
            productcategory.setName(name);
        } else {
            throw new DAONotFoundException("The product category has not been found");
        }

    }

    @Override
    public void updateStockQuantity(Long idProductstore, Long idProduct, Integer quantity) throws DAONotFoundException {
        StockBean stock = StockDAO.getByIdProductstoreAndIdProduct(idProduct, idProductstore);
        if (stock != null) {
            stock.setQuantity(quantity);
            StockDAO.updateStock(stock);
        } else {
            throw new DAONotFoundException("The stock has not been found");
        }

    }

    @Override
    public void removeProduct(Long idProduct) throws DAONotFoundException {
        ProductBean product = ProductDAO.getByIdProduct(idProduct);
        if (idProduct != null) {
            ProductDAO.deleteProduct(product);
        } else {
            throw new DAONotFoundException("The product has not been found");
        }
    }

    @Override
    public void removeProductstore(Long idProductstore) throws DAONotFoundException {
        ProductstoreBean productstore = ProductstoreDAO.getByIdProductstore(idProductstore);
        if (productstore != null) {
            ProductstoreDAO.deleteProductstore(productstore);
        } else {
            throw new DAONotFoundException("The productstore has not been found");
        }
    }

    @Override
    public void removeProductcategory(Long idProductcategory) throws DAONotFoundException {
        ProductcategoryBean productcategory = ProductcategoryDAO.getByIdProductcategory(idProductcategory);
        if (productcategory != null) {
            ProductcategoryDAO.deleteProductcategory(productcategory);
        } else {
            throw new DAONotFoundException("The productcategory has not been found");
        }
    }

    @Override
    public ProductcategoryBean getProductcategoryByName(String name) throws DAONotUniqueException {
        return ProductcategoryDAO.getByName(name);
    }

    @Override
    public Set<ProductBean> getAllProducts(String order) {
        return ProductDAO.getAllProducts(order);
    }
}
